import yaml

from common.GetFilePath import filePath


class YamlUtil(object):
    def __init__(self, file_dir, file_name):
        self.file_dir = file_dir
        self.file_name = file_name

    def read_yaml(self):
        with open(filePath(self.file_dir, self.file_name), 'r', encoding='utf-8') as f:
            return list(yaml.safe_load_all(f))


    def clear_yaml(self,data):
        #历史的yaml文件内容会被清空，使用mode = "w"  ,mode="a"为追加
        with open(filePath(self.file_dir, self.file_name), 'w', encoding='utf-8') as W_f:
            yaml.dump(data,stream=W_f,allow_unicode=True)


    def write_yaml(self, data):
        # 历史的yaml文件内容会被清空，使用mode = "w"  ,mode="a"为追加
        with open(filePath(self.file_dir, self.file_name), 'a', encoding='utf-8') as W_f:
            yaml.dump(data, stream=W_f, allow_unicode=True)


if __name__ == '__main__':


    print(YamlUtil('config', 'config_user.yaml').read_yaml()[0]["ip"])


